//
//  HYOUGiftHomeViewController.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUGiftHomeViewController.h"
#import "HYOUTitleName.h"
#import "HYOUChangeTitleView.h"
#import "HYOUGiftHomeCollectionCell.h"
#import "HYOUHomeMoreCollectionCell.h"
#import "HYOUWebViewController.h"
#import "HYOUScrollWebViewController.h"
#import "HYOUHeadDetailViewController.h"

@interface HYOUGiftHomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,HYOUChangeTitleViewDelegate,HYOUHomeMoreCollectionCellDelegate,HYOUGiftHomeCollectionCellDelegate>

@property (nonatomic, retain)NSMutableArray *nameArray;
@property (nonatomic, retain)NSMutableArray *allArray;
@property (nonatomic, retain)NSMutableArray *collectionArray;

@property (nonatomic, retain)HYOUBaseCollectionView *collectionView;

@property (nonatomic, retain)HYOUChangeTitleView *change;

//@property(nonatomic ,retain) HYOUDownScrollViewCell *down;

@end

@implementation HYOUGiftHomeViewController
//-(void)dealloc{
//    [_bigArray release];
//    [_bigDictionary release];
//    [_nameArray release];
//    [_allArray release];
//    [_collectionArray release];
//    [_collectionView release];
//    [_change release];
//    [super dealloc];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel *naviTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    naviTitle.text = @"礼物说";
    naviTitle.font = [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:20];
    self.navigationItem.titleView = naviTitle;
    [naviTitle release];
    
    UIImage *image = [[UIImage imageNamed:@"iconfont-jycxun"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(searchAction:)];
    
    //app字体
    [self setFontFamily:@"FZLTXHK--GBK1-0" forView:self.view andSubViews:YES];
    
    
    self.nameArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    
    self.bigDictionary = [NSMutableDictionary dictionary];
    self.bigArray = [NSMutableArray array];
    
    NSString *string = @"http://api.liwushuo.com/v2/channels/preset?gender=2&generation=2";
#pragma makr -- 审核 block
    [AppTools finalAFNWithURLString:string parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = resuposeObject;
        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
        NSMutableArray *channels = [data objectForKey:@"channels"];
        for (NSDictionary *dic in channels) {
            HYOUTitleName *title = [[HYOUTitleName alloc] init];
            [title setValuesForKeysWithDictionary:dic];
            [self.nameArray addObject:title.name];
            [self.allArray addObject:title];
            
            NSMutableArray *itemsArray = [NSMutableArray array];
            
            [AppTools finalAFNWithURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=0&gender=2&generation=2",title.id] parameters:nil successAFNBlock:^(id resuposeObject) {
                NSMutableDictionary *bigDic = resuposeObject;
                NSMutableDictionary *data = [bigDic objectForKey:@"data"];
                NSMutableArray *items = [data objectForKey:@"items"];
                for (NSDictionary *dic in items) {
                    HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    [itemsArray addObject:model];
                }
                NSString *ID = title.id;
                NSDictionary *itemDic = [NSDictionary dictionaryWithObjectsAndKeys:itemsArray, @"collectionArray",ID,@"ID", nil];
                [self.bigArray addObject:itemDic];
                [self.collectionView reloadData];
            }];
        }
        //        创建小导航栏
        self.change = [[HYOUChangeTitleView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 44) titleArray:self.nameArray titleColor:[UIColor blackColor] titleSelectedColor:[UIColor redColor] lineColor:[UIColor redColor] sizeOfFont:15];
        [self.view addSubview:self.change];
        self.change.delegate = self;
        
    }];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT + 15);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView = [[HYOUBaseCollectionView alloc]initWithFrame:CGRectMake(0, 44, WIDTH, HEIGHT - 15) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.tag = 1000;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[HYOUGiftHomeCollectionCell class] forCellWithReuseIdentifier:@"home"];
    [self.collectionView registerClass:[HYOUHomeMoreCollectionCell class] forCellWithReuseIdentifier:@"homeMoreCell"];
    
    
//    UIImage *rightImage = [UIImage imageNamed:@"iconfont-sousuo-2.png"];
//    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:rightImage style: UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)]autorelease];
//    
//    UIImage *leftImage = [UIImage imageNamed:@"iconfont-sousuo-2.png"];
//    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:leftImage style: UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)]autorelease];

}
-(void)getPresentTitleNumber:(NSInteger)titleNumber{
    self.collectionView.contentOffset = CGPointMake(self.view.frame.size.width * (titleNumber - 1), 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag == 1000) {
        NSInteger count = scrollView.contentOffset.x/WIDTH;
        [self.change changeTitleByValue:count + 1];
    }
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        HYOUGiftHomeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"home" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    HYOUHomeMoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeMoreCell" forIndexPath:indexPath];
    HYOUTitleName *name = self.allArray[indexPath.item];
    cell.delegate = self;
    NSString *ID2 = name.id;
    cell.titleId = ID2;
//    NSLog(@"%@",ID2);
//    NSString *ID3 = [ID2 description];
//    NSLog(@"%@",name.id);
//    for (NSDictionary *dic in self.bigArray) {
//        NSString *ID = [dic objectForKey:@"ID"];
//        NSString *ID1 = [ID description];
//        if ([ID1 isEqualToString:ID3]) {
//            NSMutableArray *array = [dic objectForKey:@"collectionArray"];
//            cell.allArray = array;
//            NSLog(@"%ld",array.count);
//        }
//    }
    return cell;
}
//字体实现
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
    if ([view isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)view;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
    }
    if (isSubViews)
    {
        for (UIView *sview in view.subviews)
        {
            [self setFontFamily:fontFamily forView:sview andSubViews:YES];
        }
    }
}
-(void)showCellWebView:(NSString *)url Image:(NSString *)image title:(NSString *)title{
    HYOUWebViewController *web = [[HYOUWebViewController alloc] init];
    web.url = url;
    web.image = image;
    web.name = title;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)showHomeCellWebView:(NSString *)url Image:(NSString *)image title:(NSString *)title{
    HYOUWebViewController *web = [[HYOUWebViewController alloc] init];
    web.url = url;
    web.image = image;
    web.name = title;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)showDownScroll:(NSString *)url{
    HYOUScrollWebViewController *web = [[HYOUScrollWebViewController alloc] init];
    web.url = url;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)showDownTopic:(NSString *)url{
    HYOUHeadDetailViewController *head = [[HYOUHeadDetailViewController alloc]init];
    head.url = url;
    [self.navigationController pushViewController:head animated:YES];
}
-(void)showHeadWeb:(NSString *)url{
    HYOUWebViewController *web = [[HYOUWebViewController alloc] init];
    web.url = url;
    [self.navigationController pushViewController:web animated:YES];
}
-(void)showHeadDetail:(NSString *)targetId{
    HYOUHeadDetailViewController *head = [[HYOUHeadDetailViewController alloc] init];
    head.url = targetId;
    NSLog(@"%@",targetId);
    [self.navigationController pushViewController:head animated:YES];
    
}
-(void)searchAction:(UIBarButtonItem *)btn{
    HYOUSearchViewController *search = [[HYOUSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
